<template>
  <div class="card-container">
    <!-- 搜索区域 -->
    <div class="search-container">
      <span class="search-label">一体杆名称:</span>
      <el-input
        clearable
        placeholder="请输入内容"
        v-model="form.poleName"
        class="search-main"
      />
      <span class="search-label">一体杆编号:</span>
      <el-input
        clearable
        placeholder="请输入内容"
        v-model="form.poleNumber"
        class="search-main"
      />
      <span class="search-label">状态：</span>
      <el-select v-model="form.poleStatus">
        <el-option
          v-for="item in status"
          :label="item.name"
          :value="item.id"
          :key="item.id"
        />
      </el-select>
      <el-button type="primary" @click="chaxun" class="search-btn"
        >查询</el-button
      >
    </div>
    <!-- 新增删除操作区域 -->
    <div class="create-container">
      <el-button type="primary" @click="addyk">添加一体杆</el-button>
      <el-button @click="plsc">批量删除</el-button>
    </div>
    <!-- 表格区域 -->
    <div class="table">
      <el-table
        style="width: 100%"
        :data="tabledata"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55"> </el-table-column>
        <el-table-column type="index" label="序号" />
        <el-table-column label="一体杆名称" prop="poleName" />
        <el-table-column label="一体杆编号" prop="poleNumber" />
        <el-table-column label="一体杆IP" prop="poleIp" />
        <el-table-column label="安装区域" prop="areaName" />
        <el-table-column label="一体杆类型" prop="poleType">
          <template slot-scope="scope">
            <span v-if="scope.row.poleType == 'entrance'">入口</span>
            <span v-if="scope.row.poleType == 'export'">出口</span>
          </template>
        </el-table-column>
        <el-table-column label="状态" prop="poleStatus">
          <template slot-scope="scope">
            <span v-if="scope.row.poleStatus == 0">正常</span>
            <span v-if="scope.row.poleStatus == 1">异常</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" fixed="right" width="180">
          <template #default="scope">
            <el-button size="mini" type="text" @click="edit(scope.row)"
              >编辑</el-button
            >
            <el-button size="mini" type="text" @click="shanchu(scope.row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="page-container">
      <el-pagination
        layout="total, prev, pager, next"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="form.page"
        :page-size="form.pageSize"
      />
    </div>
    <!-- 添加楼宇 -->
    <el-dialog
      :title="title"
      :visible="dialogVisible"
      width="580px"
      @close="closeDialog"
    >
      <!-- 表单接口 -->
      <div class="form-container">
        <el-form ref="addForm" :model="addForm" :rules="addFormRules">
          <el-form-item label="一体杆名称" prop="poleName">
            <el-input v-model="addForm.poleName" />
          </el-form-item>
          <el-form-item label="一体杆编号" prop="poleNumber">
            <el-input v-model="addForm.poleNumber" />
          </el-form-item>
          <el-form-item label="一体杆IP" prop="poleIp">
            <el-input v-model="addForm.poleIp" />
          </el-form-item>
          <el-form-item label="关联区域" prop="areaId">
            <el-select v-model="addForm.areaId">
              <el-option
                v-for="item in list"
                :label="item.name"
                :value="item.id"
                :key="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="一体杆类型" prop="poleType">
            <el-select v-model="addForm.poleType">
              <el-option label="入口" value="entrance"></el-option>
              <el-option label="出口" value="export"></el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </div>
      <template #footer>
        <el-button size="mini" @click="dialogVisible = false">取 消</el-button>
        <el-button size="mini" type="primary" @click="submit">确 定</el-button>
      </template>
    </el-dialog>
  </div>
</template>


<script>
import {
  getytg,
  yuekalb,
  deleteytg,
  editytg,
  addytg,
  qylylist,
  yuekashanchu,
  yuekatip,
} from "@/api/user";

export default {
  data() {
    return {
      form: {
        page: 1,
        pageSize: 10,
      },
      title: "添加一体杆",
      obj: {},
      tabledata: [],
      dialogVisible: false,
      selection: [],
      addForm: {},
      total: 0,
      list: [],
      addFormRules: {
        poleName: [
          { required: true, message: "请输入一体杆名称", trigger: "blur" },
        ],
        poleNumber: [
          { required: true, message: "请输入一体杆编号", trigger: "blur" },
        ],
        poleIp: [
          { required: true, message: "请输入一体杆IP", trigger: "blur" },
        ],
        areaId: [
          { required: true, message: "请输入安装区域", trigger: "change" },
        ],
        poleType: [
          { required: true, message: "请输入一体杆类型", trigger: "change" },
        ],
      },
      status: [
        { id: 0, name: "正常" },
        { id: 1, name: "异常" },
      ],
    };
  },
  mounted() {
    this.getdata();
    this.getlist();
  },
  methods: {
    submit() {
      this.$refs.addForm.validate(async (valid) => {
        if (valid) {
          if (this.title == "添加一体杆") {
            const res = await addytg(this.addForm);
            if (res.code == 10000) {
              this.$message.success("成功");
              this.getdata();
              this.dialogVisible = false;
            }
          } else {
            delete this.addForm.areaName;
            delete this.addForm.poleStatus;
            const res = await editytg(this.addForm);
            if (res.code == 10000) {
              this.$message.success("成功");
              this.getdata();
              this.dialogVisible = false;
            }
          }
        }
      });
    },
    async getlist() {
      const res = await qylylist();
      console.log(res, "ressss");
      this.list = res.data;
    },
    closeDialog() {
      this.dialogVisible = false;
    },
    plsc() {
      if (this.selection.length == 0) {
        this.$message.warning("请选择你要删除的数据");
      } else {
        let arr = [];
        this.selection.forEach((el) => {
          arr.push(el.id);
        });
        this.shanchu(arr.join(","));
      }
    },

    shanchu(id) {
      this.$confirm("是否确认删除?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          const res = await deleteytg(id);
          if (res.code == 10000) {
            this.$message({
              type: "success",
              message: "删除成功!",
            });
            this.getdata();
          } else {
            this.$message({
              type: "info",
              message: res.msg,
            });
          }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    handleSelectionChange(e) {
      console.log(e, "e");
      this.selection = e;
    },
    xf(id) {
      this.$router.push({
        path: "/carxf",
        query: {
          id: id,
        },
      });
    },
    addyk() {
      this.dialogVisible = true;
      this.addForm = {};

      this.title = "添加一体杆";
    },
    xq(id) {
      this.$router.push({
        path: "/carxq",
        query: {
          id: id,
        },
      });
    },
    edit(row) {
      this.addForm = JSON.parse(JSON.stringify(row));
      this.title = "编辑一体杆";
      this.dialogVisible = true;
    },
    handleSizeChange(e) {
      this.form.pageSize = e;
      this.getdata();
    },
    handleCurrentChange(e) {
      this.form.page = e;
      this.getdata();
    },
    chaxun() {
      this.form.page = 1;
      this.form.pageSize = 10;
      this.getdata();
    },
    async getdata() {
      const res = await getytg(this.form);
      console.log(res, "res");
      this.total = res.data.total;
      this.tabledata = res.data.rows;
    },
  },
};
</script>


<style lang="scss" scoped>
.card-container {
  padding: 20px;
  background-color: #fff;
}

.search-container {
  display: flex;
  align-items: center;
  border-bottom: 1px solid rgb(237, 237, 237, 0.9);
  padding-bottom: 20px;
  .search-main {
    width: 220px;
    margin-right: 10px;
  }
  .search-btn {
    margin-left: 20px;
  }
}
.create-container {
  margin: 10px 0px;
}
.page-container {
  padding: 4px 0px;
  text-align: right;
}
.form-container {
  padding: 0px 80px;
}
</style>